import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getAllAccessName } from '@/utils/access'
import { resetRouter } from '@/router'

var user = {
  namespaced: true,
  state: {
    token: '',
    name: '',
    avatar: '',
    access: []
  },
  mutations: {
    resetState: (state) => {
      var tempState = {
        token: getToken(),
        name: '',
        avatar: '',
        access: []
      }
      Object.assign(state, tempState)
    },
    setToken: (state, token) => {
      state.token = token
    },
    setName: (state, name) => {
      state.name = name
    },
    setAvatar: (state, avatar) => {
      state.avatar = avatar
    },
    setAccess: (state, access) => {
      state.access = access
    }
  },
  actions: {
    // user login
    login({ commit }, parms) {
      var userInfo = parms.loginform
      var remeberMe = parms.remberMe
      // const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        // var user = { username: username.trim(), password: password }
        login(userInfo).then(response => {
          const res = response
          if (res.code === 1000) {
            var token = res.data.token
            commit('setToken', token)
            // commit('setAccess', data.access)
            setToken(token, remeberMe)
            resolve()
          } else {
            this.$message.error(res.msg)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // get user info
    getInfo({ commit, dispatch, state }) {
      return new Promise((resolve, reject) => {
        var token = getToken()
        getInfo(token).then(response => {
          console.log(response)
          const { data } = response
          if (!data) {
            return reject('验证失败，请重新登录！')
          }

          var { name, avatar, access, roles } = data

          // 如果是管理员，则给予全部权限
          if (roles.indexOf('管理员') !== -1) {
            var accessNameAll = getAllAccessName()
            access = access.concat(accessNameAll)
            data.access = access
          }

          var avatarUrl = avatar.indexOf('http') !== -1 ? avatar : process.env.VUE_APP_BASE_API + avatar

          commit('setName', name)
          commit('setAvatar', avatarUrl)
          commit('setAccess', access)
          // 根据传入的权限，更新状态管理器中的有权限浏览的菜单
          dispatch('menu/getAccessMenu', access, { root: true })
          // dispath('menu/getAccessMenu', access, {root: true})
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // user logout
    logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('resetState')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // remove token
    resetToken({ commit }) {
      return new Promise(resolve => {
        removeToken() // must remove  token  first
        commit('resetState')
        resolve()
      })
    }
  }
}

export default user

